package com.shanghai.shanghaielectric.DataAdapter;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.Model.ReceiveDataModel;
import com.shanghai.shanghaielectric.Model.THD_ChartModel;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import shanghaielectric_lib.RDBHelper.Cell_show;
import shanghaielectric_lib.Utils.ByteUtils;
import shanghaielectric_lib.Utils.DoubleUtils;
import shanghaielectric_lib.Utils.LogUtil;
import shanghaielectric_lib.Utils.MyArrayUtils;
import shanghaielectric_lib.Utils.dwin.BitUtils;

/**
 * Created by 45000460 on 2017/11/24.
 */

public class Cells2ShowAdapter {
    public ReceiveDataModel cells2ReceiveDataModel(Map<Integer, Cell_show> cellShowMap) {

        ReceiveDataModel receiveDataModel = new ReceiveDataModel();

        receiveDataModel.capacityIndex = Integer.valueOf(cellShowMap.get(3).showValue);
        receiveDataModel.capacityCuttingTimes = cellShowMap.get(4).showValue;

        receiveDataModel.grid_voltage_a = cellShowMap.get(5).showValue;
        receiveDataModel.grid_voltage_b = cellShowMap.get(6).showValue;
        receiveDataModel.grid_voltage_c = cellShowMap.get(7).showValue;

        receiveDataModel.grid_current_a = cellShowMap.get(14).showValue;
        receiveDataModel.grid_current_b = cellShowMap.get(15).showValue;
        receiveDataModel.grid_current_c = cellShowMap.get(16).showValue;

        receiveDataModel.grid_THD_a = cellShowMap.get(18).showValue;
        receiveDataModel.grid_THD_b = cellShowMap.get(19).showValue;
        receiveDataModel.grid_THD_c = cellShowMap.get(20).showValue;

        receiveDataModel.grid_active_power_a = cellShowMap.get(59).showValue;
        receiveDataModel.grid_active_power_b = cellShowMap.get(60).showValue;
        receiveDataModel.grid_active_power_c = cellShowMap.get(61).showValue;

        receiveDataModel.grid_reactive_power_a = cellShowMap.get(62).showValue;
        receiveDataModel.grid_reactive_power_b = cellShowMap.get(63).showValue;
        receiveDataModel.grid_reactive_power_c = cellShowMap.get(64).showValue;

        receiveDataModel.load_current_a = cellShowMap.get(37).showValue;
        receiveDataModel.load_current_b = cellShowMap.get(38).showValue;
        receiveDataModel.load_current_c = cellShowMap.get(39).showValue;

        receiveDataModel.load_THD_a = cellShowMap.get(41).showValue;
        receiveDataModel.load_THD_b = cellShowMap.get(42).showValue;
        receiveDataModel.load_THD_c = cellShowMap.get(43).showValue;

        receiveDataModel.load_power_factor_a = cellShowMap.get(65).showValue;
        receiveDataModel.load_power_factor_b = cellShowMap.get(66).showValue;
        receiveDataModel.load_power_factor_c = cellShowMap.get(67).showValue;

        receiveDataModel.equipment_current_a = cellShowMap.get(56).showValue;
        receiveDataModel.equipment_current_b = cellShowMap.get(57).showValue;
        receiveDataModel.equipment_current_c = cellShowMap.get(58).showValue;

//电网有功功率
        double grid_active_power_a = (double) cellShowMap.get(59).realValue;
        double grid_active_power_b = (double) cellShowMap.get(60).realValue;
        double grid_active_power_c = (double) cellShowMap.get(61).realValue;
//电网无功功率
        double grid_reactive_power_a = (double) cellShowMap.get(62).realValue;
        double grid_reactive_power_b = (double) cellShowMap.get(63).realValue;
        double grid_reactive_power_c = (double) cellShowMap.get(64).realValue;


        //计算值电网功率因数  Pa÷√(Pa^2+Qa^2 )   保留2位小数
        receiveDataModel.grid_PF_a = String.valueOf(DoubleUtils.formatDoubleData(getPowerFactor(grid_active_power_a, grid_reactive_power_a), 2));
        receiveDataModel.grid_PF_b = String.valueOf(DoubleUtils.formatDoubleData(getPowerFactor(grid_active_power_b, grid_reactive_power_b), 2));
        receiveDataModel.grid_PF_c = String.valueOf(DoubleUtils.formatDoubleData(getPowerFactor(grid_active_power_c, grid_reactive_power_c), 2));


        //计算补偿电流负载率   A相柜机电流/系统总有效容量 以%显示，保留1位小数
        if (Constants.totalCapacity != 0) {
            receiveDataModel.equipment_load_factor_a = String.valueOf(DoubleUtils.formatDoubleData(((double) cellShowMap.get(56).realValue / (double) Constants.totalCapacity), 1));
            receiveDataModel.equipment_load_factor_b = String.valueOf(DoubleUtils.formatDoubleData(((double) cellShowMap.get(57).realValue / (double) Constants.totalCapacity), 1));
            receiveDataModel.equipment_load_factor_c = String.valueOf(DoubleUtils.formatDoubleData(((double) cellShowMap.get(58).realValue / (double) Constants.totalCapacity), 1));
        }
        double all_active_power = grid_active_power_a + grid_active_power_b + grid_active_power_c;
        double all_reactive_power = grid_reactive_power_a + grid_reactive_power_b + grid_reactive_power_c;


        //系统有功功率  P=Pa+Pb+Pc
        receiveDataModel.all_active_power = String.valueOf(DoubleUtils.formatDoubleData(all_active_power, 2));
        //系统无功功率  Q=Qa+Qb+Qc      注意有符号数
        receiveDataModel.all_reactive_power = String.valueOf(DoubleUtils.formatDoubleData(all_reactive_power, 2));
        // 系统功率因数  P÷√(P^2+Q^2 )      保留2位小数
        receiveDataModel.all_PF = String.valueOf(DoubleUtils.formatDoubleData(getPowerFactor(all_active_power, all_reactive_power), 2));


        receiveDataModel.frequence = cellShowMap.get(8).showValue;
        try {
            receiveDataModel.systemState = "0".equals(cellShowMap.get(1).showValue) ? cellShowMap.get(2).showValue : cellShowMap.get(1).showValue;
            LogUtil.d("systemState", "" + receiveDataModel.systemState);
            LogUtil.d("systemState-1", "" + cellShowMap.get(1).showValue);
            LogUtil.d("systemState-2", "" + cellShowMap.get(2).showValue);
        } catch (Exception e) {
            LogUtil.e("systemState", "" + e.toString());
        }

        if ("运行".equals(cellShowMap.get(2).showValue)) {
            receiveDataModel.reactiveStateCode = "0x0002";
        } else if ("待机".equals(cellShowMap.get(2).showValue)) {
            receiveDataModel.reactiveStateCode = "0x0001";
        }
        if ("故障".equals(cellShowMap.get(2).showValue)) {
            receiveDataModel.reactiveStateCode = "0x0F01";
        }

//        receiveDataModel.reactiveStateCode="0x"+ MyArrayUtils.byteArrayToHexStr(MyArrayUtils.intToByteArray_2(Integer.valueOf(cellShowMap.get(2).showValue)));

        receiveDataModel.UdcP = cellShowMap.get(79).showValue;
        receiveDataModel.UdcN = cellShowMap.get(80).showValue;


        receiveDataModel.stationID = String.valueOf((Integer.valueOf(cellShowMap.get(11).showValue) >> 1) & (7));

        receiveDataModel.capacity = cellShowMap.get(9).showValue;

        // TODO: 2017/11/30  风机状态未确定
        receiveDataModel.unite_FJ_state = BitUtils.getBit(Integer.valueOf(cellShowMap.get(12).showValue), 1);

        receiveDataModel.IGBT_A = cellShowMap.get(81).showValue;
        receiveDataModel.IGBT_B = cellShowMap.get(82).showValue;
        receiveDataModel.IGBT_C = cellShowMap.get(83).showValue;

        receiveDataModel.errorCode_1 = "0x" + ByteUtils.byteArrayToHexStr(MyArrayUtils.intToByteArray_2(Integer.valueOf(cellShowMap.get(84).showValue)));
        receiveDataModel.errorCode_2 = "0x" + ByteUtils.byteArrayToHexStr(MyArrayUtils.intToByteArray_2(Integer.valueOf(cellShowMap.get(85).showValue)));
        receiveDataModel.errorCode_3 = "0x" + ByteUtils.byteArrayToHexStr(MyArrayUtils.intToByteArray_2(Integer.valueOf(cellShowMap.get(86).showValue)));
        receiveDataModel.errorCode_4 = "0x" + ByteUtils.byteArrayToHexStr(MyArrayUtils.intToByteArray_2(Integer.valueOf(cellShowMap.get(87).showValue)));
        receiveDataModel.errorCode_5 = "0x" + ByteUtils.byteArrayToHexStr(MyArrayUtils.intToByteArray_2(Integer.valueOf(cellShowMap.get(88).showValue)));

        receiveDataModel.alertCode_1 = "0x" + ByteUtils.byteArrayToHexStr(MyArrayUtils.intToByteArray_2(Integer.valueOf(cellShowMap.get(68).showValue)));
        receiveDataModel.alertCode_2 = "0x" + ByteUtils.byteArrayToHexStr(MyArrayUtils.intToByteArray_2(Integer.valueOf(cellShowMap.get(69).showValue)));
        receiveDataModel.alertCode_3 = "0x" + ByteUtils.byteArrayToHexStr(MyArrayUtils.intToByteArray_2(Integer.valueOf(cellShowMap.get(70).showValue)));
        receiveDataModel.alertCode_4 = "0x" + ByteUtils.byteArrayToHexStr(MyArrayUtils.intToByteArray_2(Integer.valueOf(cellShowMap.get(71).showValue)));

//        receiveDataModel.alertCode_1 = "0x" + Integer.toHexString(Integer.valueOf(cellShowMap.get(68).showValue));
//        receiveDataModel.alertCode_2 = "0x" + Integer.toHexString(Integer.valueOf(cellShowMap.get(69).showValue));
//        receiveDataModel.alertCode_3 = "0x" + Integer.toHexString(Integer.valueOf(cellShowMap.get(70).showValue));
//        receiveDataModel.alertCode_4 = "0x" + Integer.toHexString(Integer.valueOf(cellShowMap.get(71).showValue));

        receiveDataModel.errorCode_1_real = Integer.valueOf(cellShowMap.get(84).showValue);
        receiveDataModel.errorCode_2_real = Integer.valueOf(cellShowMap.get(85).showValue);
        receiveDataModel.errorCode_3_real = Integer.valueOf(cellShowMap.get(86).showValue);
        receiveDataModel.errorCode_4_real = Integer.valueOf(cellShowMap.get(87).showValue);
        receiveDataModel.errorCode_5_real = Integer.valueOf(cellShowMap.get(88).showValue);

        receiveDataModel.alertCode_1_real = Integer.valueOf(cellShowMap.get(68).showValue);
        receiveDataModel.alertCode_2_real = Integer.valueOf(cellShowMap.get(69).showValue);
        receiveDataModel.alertCode_3_real = Integer.valueOf(cellShowMap.get(70).showValue);
        receiveDataModel.alertCode_4_real = Integer.valueOf(cellShowMap.get(71).showValue);

        int di = Integer.valueOf(cellShowMap.get(11).showValue);
        receiveDataModel.DI = Integer.valueOf(cellShowMap.get(11).showValue);
        receiveDataModel.unite_FJ_state = BitUtils.getBit(Integer.valueOf(cellShowMap.get(12).showValue), 1);

        receiveDataModel.DIO_1 = Integer.valueOf(cellShowMap.get(12).showValue);
        receiveDataModel.DIO_2 = Integer.valueOf(cellShowMap.get(13).showValue);

        LogUtil.e("di", "" + receiveDataModel.DI);
        LogUtil.e("diO1", "" + receiveDataModel.DIO_1);
        LogUtil.e("diO2", "" + receiveDataModel.DIO_2);

        receiveDataModel.harmonicN = Integer.valueOf(cellShowMap.get(21).showValue);
        receiveDataModel.Agrid_N = (double) cellShowMap.get(22).realValue;
        receiveDataModel.Bgrid_N = (double) cellShowMap.get(23).realValue;
        receiveDataModel.Cgrid_N = (double) cellShowMap.get(24).realValue;

        receiveDataModel.Aload_N = (double) cellShowMap.get(25).realValue;
        receiveDataModel.Bload_N = (double) cellShowMap.get(26).realValue;
        receiveDataModel.Cload_N = (double) cellShowMap.get(27).realValue;


        THD_ChartModel thd_chartModel = new THD_ChartModel();
        thd_chartModel.setThd_index(receiveDataModel.harmonicN);
        thd_chartModel.setThd_data_1(receiveDataModel.Agrid_N);
        thd_chartModel.setThd_data_2(receiveDataModel.Bgrid_N);
        thd_chartModel.setThd_data_3(receiveDataModel.Cgrid_N);
        thd_chartModel.setThd_data_4(receiveDataModel.Aload_N);
        thd_chartModel.setThd_data_5(receiveDataModel.Bload_N);
        thd_chartModel.setThd_data_6(receiveDataModel.Cload_N);

        EventBus.getDefault().post(thd_chartModel);
        return receiveDataModel;
    }

    public double getPowerFactor(double P, double Q) {
        if (P != 0 | P != 0) {
            return P / Math.sqrt((P * P + Q * Q));
        } else {
            return 0;
        }
    }

}
